// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets;

import java.nio.ByteBuffer;
import zombie.characters.IsoPlayer;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.network.GameClient;
import zombie.network.GameServer;
import zombie.network.packets.hit.Instance;

public class PlayerID extends Instance implements INetworkPacket {
    protected IsoPlayer player;
    protected byte playerIndex;

    public void set(IsoPlayer _player) {
        super.set(_player.OnlineID);
        this.playerIndex = _player.isLocal() ? (byte)_player.getPlayerNum() : -1;
        this.player = _player;
    }

    public void parsePlayer(UdpConnection connection) {
        if (GameServer.bServer) {
            if (connection != null && this.playerIndex != -1) {
                this.player = GameServer.getPlayerFromConnection(connection, this.playerIndex);
            } else {
                this.player = (IsoPlayer)GameServer.IDToPlayerMap.get(this.ID);
            }
        } else if (GameClient.bClient) {
            this.player = (IsoPlayer)GameClient.IDToPlayerMap.get(this.ID);
        }
    }

    @Override
    public void parse(ByteBuffer b, UdpConnection connection) {
        super.parse(b, connection);
        this.playerIndex = b.get();
    }

    @Override
    public void write(ByteBufferWriter b) {
        super.write(b);
        b.putByte(this.playerIndex);
    }

    @Override
    public boolean isConsistent() {
        return super.isConsistent() && this.getCharacter() != null;
    }

    @Override
    public String getDescription() {
        return super.getDescription() + "\n\tPlayer [ player " + (this.player == null ? "?" : "\"" + this.player.getUsername() + "\"") + " ]";
    }

    public IsoPlayer getCharacter() {
        return this.player;
    }
}
